home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\MAIL.C < prev    next >
C/C++ Source or Header  |  1994-12-31  |  7KB  |  301 lines

  1. /*
  2.  * mail.c: Ok, so I gave in.  I added mail checking.  So sue me. 
  3.  *
  4.  * Written By Michael Sandrof
  5.  *
  6.  * Copyright(c) 1990 
  7.  *
  8.  * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
  9.  */
  10.  
  11. #ifndef lint
  12. static    char    rcsid[] = "@(#)$Id: mail.c,v 1.16 1994/07/30 17:31:29 mrg Stab $";
  13. #endif
  14.  
  15. #define MAIL_TWO
  16.  
  17. #include "irc.h"
  18.  
  19. /* stuff from gnu autoconf docs */
  20.  
  21. #if defined (DIRENT) || defined(_POSIX_SOURCE)
  22. # include <dirent.h>
  23. # define NLENGTH(d) (strlen((d)->d_name)
  24. #else /* DIRENT || _POSIX_SOURCE */
  25. # define drient direct
  26. # define NLENGTH(d) ((d)->d_namlen)
  27. # ifdef SYSNDIR
  28. #  include <sys/ndir.h>
  29. # endif /* SYSNDIR */
  30. # ifdef SYSDIR
  31. #  include <sys/dir.h>
  32. # endif /* SYSDIR */
  33. # ifdef NDIR
  34. #  include <ndir.h>
  35. # endif /* NDIR */
  36. #endif /* DIRENT || _POSIX_VERSION */
  37.  
  38. #ifdef HAVE_FCNTL_H
  39. # include <fcntl.h>
  40. #endif /* HAVE_FCNTL_H */
  41.  
  42. #ifdef ESIX
  43. # include <lan/net_types.h>
  44. #endif /* !ESIX */
  45.  
  46. /* #if (defined(ISC) || defined(POSIX) || defined (UNICOS)) && !defined(_IBMR2) */
  47. #ifdef HAVE_SYS_FCNTL_H
  48. # include <sys/fcntl.h>
  49. #endif /* HAVE_SYS_FCNTL_H */
  50.  
  51. #include <sys/stat.h>
  52.  
  53. #include "mail.h"
  54. #include "lastlog.h"
  55. #include "hook.h"
  56. #include "vars.h"
  57. #include "ircaux.h"
  58. #include "output.h"
  59. #include "window.h"
  60.  
  61. static    char    *mail_path = (char *) 0;
  62.  
  63. /* init_mail: this initialized the path to the users mailbox */
  64. static    void
  65. init_mail()
  66. {
  67.     char    *tmp_mail_path;
  68.  
  69.     if (mail_path)
  70.         return; /* why do it 2000 times?  -lynx */
  71.  
  72. #ifdef UNIX_MAIL
  73.     if ((tmp_mail_path = getenv("MAIL")) != NULL)
  74.         strmcpy(buffer, tmp_mail_path, BIG_BUFFER_SIZE);
  75.                 /*then use it - Goodi */
  76.     else
  77.     {
  78.         strmcpy(buffer, UNIX_MAIL, BIG_BUFFER_SIZE);
  79.         strmcat(buffer, "/", BIG_BUFFER_SIZE);
  80.         strmcat(buffer, username, BIG_BUFFER_SIZE);
  81.     }
  82.     malloc_strcpy(&mail_path, buffer);
  83. #else
  84. # ifdef AMS_MAIL
  85.     strmcpy(buffer, my_path, BIG_BUFFER_SIZE);
  86.     strmcat(buffer, "/", BIG_BUFFER_SIZE);
  87.     strmcat(buffer, AMS_MAIL, BIG_BUFFER_SIZE);
  88.     malloc_strcpy(&mail_path, buffer);
  89. # endif /* AMS_MAIL */
  90. #endif /* UNIX_MAIL */
  91. }
  92.  
  93. #ifdef AMS_MAIL
  94. /*
  95.  * count_files: counts all the visible files in the specified directory and
  96.  * returns that number as the function value 
  97.  */
  98. static    u_int
  99. count_files(dir_name, lasttime)
  100.     char    *dir_name;
  101.     time_t    lasttime;
  102. {
  103.     DIR    *dir;
  104.     struct    direct    *dirbuf;
  105.     unsigned int    cnt;
  106.     int    fd;
  107.     define_big_buffer(LetterName);
  108.     struct    stat    LetterInfo;
  109.     static    int    VirginProgram = 1;
  110.     int    lastlog_level;
  111.  
  112.     if ((dir = opendir(dir_name)) == (DIR *) 0)
  113.     {
  114.         free_big_buffer(LetterName);
  115.         return (0);
  116.     }
  117.     cnt = 0;
  118.     lastlog_level = set_lastlog_msg_level(LOG_CRAP);
  119.     message_from((char *) 0, LOG_CURRENT);
  120.     while ((dirbuf = readdir(dir)) != (struct direct *) 0)
  121.     {
  122.         if (*(dirbuf->d_name) != '.')
  123.         {
  124.             cnt++;
  125.             sprintf(LetterName, "%s/%s", dir_name, dirbuf->d_name);
  126.             stat_file(LetterName, &LetterInfo);
  127.             if (get_int_var(MAIL_VAR) == 2 && LetterInfo.st_ctime>lasttime && !VirginProgram)
  128.             {
  129.                 if ((fd = open(LetterName, O_RDONLY)) == -1)
  130.                     say("Unable to check headers on new mail");
  131.                 else
  132.                 {
  133.                     while (dgets(LetterName,BIG_BUFFER_SIZE, fd, (char *) 0) > 0 && *LetterName != '\n' &&
  134.                         *LetterName != '\0')
  135.                     {
  136.                         LetterName[strlen(LetterName) - - 1] = '\0';
  137.                         if (!strncmp(LetterName, "From", 4) || !strncmp(LetterName, "Subject:", 8))
  138.                             say("%s", LetterName);
  139.                     }
  140.                     new_close(fd);
  141.                 }
  142.             }
  143.         }
  144.     }
  145. end:
  146.     VirginProgram = 0;
  147.     closedir(dir);
  148.     set_lastlog_msg_level(lastlog_level);
  149.     free_big_buffer(LetterName);
  150.     return (cnt);
  151. }
  152. #endif /* AMS_MAIL */
  153.  
  154. /*
  155.  * check_mail_status: returns 0 if mail status has not changed, 1 if mail
  156.  * status has changed 
  157.  */
  158. int
  159. check_mail_status()
  160. {
  161.  
  162. #if defined(AMS_MAIL) || defined(UNIX_MAIL)
  163.     struct    stat    stat_buf;
  164.     static    time_t    old_stat = 0L;
  165.  
  166. #ifdef DAEMON_UID
  167.     if (getuid() == DAEMON_UID)
  168.         return 0;
  169. #endif /*DAEMON_UID*/
  170.  
  171.     if (!get_int_var(MAIL_VAR))
  172.     {
  173.         old_stat = 0L;
  174.         return (0);
  175.     }
  176.     init_mail();
  177.     if (stat_file(mail_path, &stat_buf) == -1)
  178.         return (0);
  179.     if (stat_buf.st_ctime > old_stat)
  180.     {
  181.         old_stat = stat_buf.st_ctime;
  182.         return (1);
  183.     }
  184. #endif /* defined(AMS_MAIL) || defined(UNIX_MAIL) */
  185.     return (0);
  186. }
  187.  
  188. /*
  189.  * check_mail: This here thing counts up the number of pieces of mail and
  190.  * returns it as static string.  If there are no mail messages, null is
  191.  * returned. 
  192.  */
  193. char    *
  194. check_mail()
  195. {
  196.  
  197. #if !defined(AMS_MAIL) && !defined(UNIX_MAIL)
  198.     return    (char *) 0;
  199. #else
  200.     static    unsigned int    cnt = 0;
  201.     static    time_t    old_stat = 0L;
  202.     static    char    ret_str[8];
  203.     struct    stat    stat_buf;
  204.     unsigned int    new_cnt = 0;
  205.     char    tmp[8];
  206.     static    int    VirginProgram = 1;  /* It's its first time */
  207.     int    lastlog_level;
  208.  
  209. #ifdef UNIX_MAIL
  210.     int    des;
  211. #endif /* UNIX_MAIL */
  212.  
  213. #ifdef DAEMON_UID
  214.     if (getuid()==DAEMON_UID)
  215.         return ((char *) 0);
  216. #endif
  217.  
  218.     init_mail();
  219. #ifdef UNIX_MAIL
  220.     if (stat_file(mail_path, &stat_buf) == -1)
  221.         return ((char *) 0);
  222.     lastlog_level = set_lastlog_msg_level(LOG_CRAP);
  223.     message_from((char *) 0, LOG_CURRENT);
  224.     if (stat_buf.st_ctime > old_stat)
  225.     {
  226.         old_stat = stat_buf.st_ctime;
  227.         if ((des = open(mail_path, O_RDONLY, 0)) >= 0)
  228.         {
  229.             new_cnt = 0;
  230.             while (dgets(buffer, BIG_BUFFER_SIZE, des,(char *) 0)>0)
  231.             {
  232.                 if (!strncmp(MAIL_DELIMITER, buffer,
  233.                     sizeof(MAIL_DELIMITER) - 1))
  234.                 {
  235.                     new_cnt++;
  236. # ifdef MAIL_TWO
  237.                     if (new_cnt>cnt && !VirginProgram &&
  238.                         get_int_var(MAIL_VAR)==2)
  239.  
  240.                     {
  241.                         /*buffer[strlen(buffer)-1]='\0';
  242.                         say("%s", buffer); */
  243.                         while (dgets(buffer,
  244.                             BIG_BUFFER_SIZE, des,
  245.                             (char *) 0) > 0 &&
  246.                             *buffer != '\0' &&
  247.                             *buffer != '\n')
  248.  
  249.                         {
  250.                             buffer[strlen(buffer)-1]
  251.                                 = '\0';
  252.                             if (!strncmp(buffer,
  253.                                 "From:", 5) ||
  254.                                 !strncmp(buffer,
  255.                                 "Subject:", 8))
  256.                                 say("%s",
  257.                                     buffer);
  258.                         }
  259.                     }
  260. # endif
  261.                 }
  262.             }
  263.             VirginProgram=0;
  264.             new_close(des);
  265.         }
  266. #else
  267. # ifdef AMS_MAIL
  268.         if (stat_file(mail_path, &stat_buf) == -1)
  269.         {
  270.             set_lastlog_msg_level(lastlog_level);
  271.             return ((char *) 0);
  272.         }
  273.         if (stat_buf.st_ctime > old_stat)
  274.         {
  275.             new_cnt = count_files(mail_path, old_stat);
  276.             old_stat = stat_buf.st_ctime;
  277.         }
  278. # endif /* AMS_MAIL */
  279. #endif /* UNIX_MAIL */
  280.         /* yeeeeack */
  281.         sprintf(tmp, "%d", new_cnt - cnt);
  282.         sprintf(buffer, "%d", new_cnt);
  283.         if (new_cnt > cnt)
  284.         {
  285.             if (do_hook(MAIL_LIST, "%s %s", tmp, buffer) &&
  286.                 get_int_var(MAIL_VAR)==1)
  287.                 say("You have new email.");
  288.         }
  289.         cnt = new_cnt;
  290.     }
  291.     set_lastlog_msg_level(lastlog_level);
  292.     if (cnt && (cnt < 65536))
  293.     {
  294.         sprintf(ret_str, "%d", cnt);
  295.         return (ret_str);
  296.     }
  297.     else
  298.         return ((char *) 0);
  299. #endif /* !defined(AMS_MAIL) && !defined(UNIX_MAIL) */
  300. }
  301.